package com.chenxin.wms.process.simulate.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenxin.common.annotation.WmsLogs;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.enums.WmsTaskType;
import com.chenxin.common.enums.wms.TaskStatus;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.wms.domain.LocationEntity;
import com.chenxin.wms.domain.StockEntity;
import com.chenxin.wms.domain.TaskEntity;
import com.chenxin.wms.domain.WcsTaskEntity;
import com.chenxin.wms.domain.dto.DownSimulateParams;
import com.chenxin.wms.mapper.LocationMapper;
import com.chenxin.wms.mapper.StockMapper;
import com.chenxin.wms.mapper.TaskMapper;
import com.chenxin.wms.mapper.WcsTaskMapper;
import com.chenxin.wms.process.simulate.IProcDownSimulate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ProcDownSimulate implements IProcDownSimulate {
    @Autowired
    private WcsTaskMapper wcsTaskMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private LocationMapper locationMapper;

    @WmsLogs(name = "模拟下架")
    @Transactional
    @Override
    public ProcResult call(DownSimulateParams params) {
        if (params == null) {
            return ProcResult.error("down_simulate_001", "请求参数不能为空");
        }
        // 参数校验
        String contNo = params.getContNo();
        if (StringUtils.isEmpty(contNo)) {
            return ProcResult.error("down_simulate_002", "上架托盘不能为空");
        }
        String locationNo = params.getLocationNo();
        if (StringUtils.isEmpty(locationNo)) {
            return ProcResult.error("down_simulate_003", "目标位置不能为空");
        }
        Long creator = params.getCreator();
        Date workDate = new Date();
        // 判断目标托盘是否有下架的设备任务
        QueryWrapper<WcsTaskEntity> wcsTaskQW = new QueryWrapper<>();
        wcsTaskQW.eq("cont_no", contNo)
                .eq("trk_type", "2")
                .lt("trk_status", TaskStatus.DONE.getCode());
        WcsTaskEntity trkInfo = wcsTaskMapper.selectOne(wcsTaskQW);
        if (trkInfo == null) {
            return ProcResult.error("down_simulate_004", "托盘【" + contNo + "】未匹配到设备下架任务");
        }
        // 获取待下架货位信息
        QueryWrapper<LocationEntity> locationQW = new QueryWrapper<>();
        locationQW.eq("location_no", locationNo).eq("cont_no", contNo);
        LocationEntity locationInfo = locationMapper.selectOne(locationQW);
        if (locationInfo == null) {
            return ProcResult.error("down_simulate_005", "未匹配到货位【" + locationNo + "】上有托盘【" + contNo + "】的信息");
        }

        // 处理业务-更新设备任务&业务任务的信息
        trkInfo.setCurPos("库区-STAGE");
        trkInfo.setComments("模拟下架");
        trkInfo.setTrkStatus(TaskStatus.EXEC.getCode());
        trkInfo.setUpdater(creator);
        trkInfo.setUpdateDate(workDate);
        wcsTaskMapper.updateById(trkInfo);

        QueryWrapper<TaskEntity> taskQW = new QueryWrapper<>();
        taskQW.eq("from_cont_no", contNo)
                .eq("task_type", WmsTaskType.SHIPMENT_OUT.getCode())
                .lt("task_status", TaskStatus.DONE.getCode());
        List<TaskEntity> taskList = taskMapper.selectList(taskQW);

        for (TaskEntity task : taskList) {
            task.setCurPosition("库区-STAGE");
            task.setComments("模拟下架成功");
            task.setTaskStatus(TaskStatus.EXEC.getCode());
            task.setUpdater(creator);
            task.setUpdateDate(workDate);
        }
        taskMapper.updateById(taskList);

        // 处理库存的位置信息
        QueryWrapper<StockEntity> stockQW = new QueryWrapper<>();
        stockQW.eq("cont_no", contNo);
        List<StockEntity> stockList = stockMapper.selectList(stockQW);
        for (StockEntity stock : stockList) {
            stock.setLocationNo("库区-STAGE");
            stock.setUpdater(creator);
            stock.setUpdateDate(workDate);
        }
        stockMapper.updateById(stockList);
        return ProcResult.success("模拟下架成功，托盘号【" + contNo + "】自货位【" + locationNo + "】");
    }
}
